package com.kaifamiao.controller;

import com.kaifamiao.entity.Panda;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

// 被 @Controller 标注的类是控制器类，这种类的实例就是 控制器
@Controller
@RequestMapping("/panda/**")
public class PandaController {

    @Autowired
    private Logger logger;

    private List<Panda> list;
    private int counter;

    // 相当于 <bean> 的 init-method
    @PostConstruct
    public void init() {
        list = new ArrayList<>();
        Panda p = new Panda();
        p.setId( ++counter );
        p.setName( "和和" );
        p.setGender( "男" );
        p.setBirthdate( LocalDate.of( 2002, 10 , 20 ) );
        list.add( p );
        Panda a = new Panda();
        a.setId( ++counter );
        a.setName( "美美" );
        a.setGender( "女" );
        a.setBirthdate( LocalDate.of( 2004, 7 , 10 ) );
        list.add( a );
    }

    // 控制器方法 ( Handler Methods )
    // @RequestMapping(path="/add", method = RequestMethod.POST)
    @PostMapping("/add")
    @ResponseBody
    public Map<String,Object> add( @RequestBody Panda p ) {
        logger.info( "新增" );
        p.setId( ++counter );
        list.add(p);
        return Map.of( "success", true, "message", "添加成功" );
    }

    // @RequestMapping(path="/update", method = RequestMethod.PUT)
    @PutMapping("/update")
    @ResponseBody
    public Map<String,Object> update( @RequestBody Panda p ) {
        logger.info( "更新" );
        logger.info( p.getName() );
        for( Panda x : list ){
            if( x != null && x.getId().equals( p.getId() ) ) {
                x.setName( p.getName() );
                x.setGender( p.getGender() );
                x.setBirthdate( p.getBirthdate() );
                break;
            }
        }
        return Map.of( "success", true, "message", "更新成功" );
    }

    // @RequestMapping(path="/find/{id}", method = RequestMethod.GET)
    @GetMapping("/find/{id}")
    @ResponseBody
    public Panda find( @PathVariable("id") Integer id ) {
        logger.info( "查询单个" );
        for( Panda p : list ){
            if( p != null && p.getId().equals( id ) ) {
                return p;
            }
        }
        return null;
    }

    // @RequestMapping(path="/find/all", method = RequestMethod.GET)
    @GetMapping("/find/all")
    public ResponseEntity<List<Panda>> findAll() {
        logger.info( "查询所有" );
        List<Panda> body = list;
        MultiValueMap<String,String> headers = new HttpHeaders();
        headers.add("content-type", "application/json; charset=UTF-8");
        HttpStatus status = HttpStatus.OK;
        ResponseEntity<List<Panda>> entity = new ResponseEntity<>( body, headers, status ) ;
        return entity;
    }

    // @RequestMapping(path="/find/{id}", method = RequestMethod.DELETE)
    @DeleteMapping("/remove/{id}")
    @ResponseBody
    public Map<String,Object> remove( @PathVariable("id") Integer id ) {
        logger.info( "删除" );
        for(Iterator<Panda> itor = list.iterator(); itor.hasNext(); ) {
            Panda p = itor.next();
            if( p != null && p.getId().equals( id ) ) {
                itor.remove();
                break;
            }
        }
        return Map.of( "success", true, "message", "删除成功" );
    }

}
